<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网站代码提取器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: #f9f9f9;
            border-radius: 5px;
            padding: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .input-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="url"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 20px;
            border: 1px solid #ddd;
            padding: 10px;
            background-color: #fff;
            border-radius: 4px;
            min-height: 100px;
            max-height: 400px;
            overflow-y: auto;
            white-space: pre-wrap;
        }
        .loading {
            text-align: center;
            display: none;
        }
        .error {
            color: #d32f2f;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>网站代码提取器</h1>
        
        <div class="input-group">
            <label for="url">输入网站URL:</label>
            <input type="url" id="url" placeholder="https://example.com" required>
        </div>
        
        <button id="fetchBtn">提取代码</button>
        
        <div class="loading" id="loading">
            <p>正在获取代码，请稍候...</p>
        </div>
        
        <div class="error" id="error"></div>
        
        <div class="input-group">
            <label for="result">网站HTML代码:</label>
            <div id="result">代码将显示在这里...</div>
        </div>
    </div>

    <script>
        document.getElementById('fetchBtn').addEventListener('click', function() {
            const url = document.getElementById('url').value.trim();
            const resultDiv = document.getElementById('result');
            const errorDiv = document.getElementById('error');
            const loadingDiv = document.getElementById('loading');
            
            if (!url) {
                errorDiv.textContent = '请输入有效的URL';
                return;
            }
            
            // 验证URL格式
            try {
                new URL(url);
            } catch (e) {
                errorDiv.textContent = '请输入有效的URL（包含http://或https://）';
                return;
            }
            
            // 显示加载状态
            loadingDiv.style.display = 'block';
            errorDiv.textContent = '';
            resultDiv.textContent = '正在获取代码...';
            
            // 使用代理服务器绕过CORS限制
            const proxyUrl = `https://api.codetabs.com/v1/proxy/?quest=${encodeURIComponent(url)}`;
            
            fetch(proxyUrl)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应不正常');
                    }
                    return response.text();
                })
                .then(data => {
                    resultDiv.textContent = data;
                    loadingDiv.style.display = 'none';
                })
                .catch(error => {
                    errorDiv.textContent = '获取代码失败: ' + error.message;
                    resultDiv.textContent = '获取代码失败';
                    loadingDiv.style.display = 'none';
                });
        });
    </script>
</body>
</html>